package com.kili.file.facade.picture;

import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import static com.kili.file.common.IConstants.PICTURE_UPLOAD_SERVICE;


/**
 * @author Gordon
 * @version 2019/4/8
 */
@Slf4j
@Service
public class PictureUploadServiceFacade {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "pictureUploadFallBack")
    public PictureResult pictureUpload(MultiValueMap valueMap) {
        String response = restTemplate.postForObject(PICTURE_UPLOAD_SERVICE, valueMap, String.class);
        log.info("PictureUploadResult - {}", response);
        Assert.hasText(response, "Picture upload failed");
        return JSONObject.parseObject(response, PictureResult.class);
    }

    // default time out 1s
    private PictureResult pictureUploadFallBack(MultiValueMap valueMap, Throwable ex) {
        log.error("PictureUploadFallBack - ErrorMsg : {}", ex.getMessage());
        return null;
    }
}
